/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.domain.gestion;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import co.gov.medellin.sdep.sisdep.domain.BaseController;
import co.gov.medellin.sdep.sisdep.domain.MessageModalWindowController;
import co.gov.medellin.sdep.sisdep.gestion.exception.EstConceptoCaracException;
import co.gov.medellin.sdep.sisdep.gestion.exception.EstConceptoCaracRuntimeException;
import co.gov.medellin.sdep.sisdep.gestion.service.IEstConceptoCaracService;
import co.gov.medellin.sdep.sisdep.common.AppConstants;
import co.gov.medellin.sdep.sisdep.model.EstConceptoCarac;

/**
 * @author Usuario
 * 
 */
public class EstConceptoCaracController extends BaseController implements
		Serializable {

	/**
	 * generated serial version
	 */
	private static final long serialVersionUID = -3285110767384990403L;

	/** bean para el manejo del formulario de busqueda EstConceptoCarac */
	private EstConceptoCarac estConceptoCaracSearch = new EstConceptoCarac();

	/** Objeto contenedor del listado de EstConceptoCarac luego de una consulta. */
	private transient DataModel estConceptoCaracListModel;

	/** bean para el manejo del formulario de creacion/edicion. EstConceptoCarac */
	private EstConceptoCarac estConceptoCarac = new EstConceptoCarac();

	/** instancia de servicio estConceptoCaracService */
	private IEstConceptoCaracService estConceptoCaracService;

	/**
	 * Constructor por defecto, inicializa los elemento basicos del Controlador
	 */
	public EstConceptoCaracController() {
		this.estConceptoCaracListModel = new ListDataModel();
	}
	// --------------------------------------------------------------------
	// --- metodos para atender acciones init()
	// --------------------------------------------------------------------

	/**
	 * 
	 * @return String
	 */
	public String searchEstConceptoCaracByKey() {
		String msg = "";
		try {
			List valores = null;
			valores = getEstConceptoCaracService().load(1, 10,
					this.estConceptoCaracSearch);
			this.estConceptoCaracListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.estConceptoCaracListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.estConceptoCaracListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.EST_CONCEPTO_CARAC_SEARCH_VIEW;
	}
	
	/**
	 * 
	 * @return String
	 */
	public String searchEstConceptoCaracByNombre() {
		String msg = "";
		try {
			List valores = null;
			estConceptoCaracSearch.setIdVarSocioeconomica(new Long(estConceptoCaracSearch.getIdVarSocioeconomicaStr()));
			valores = getEstConceptoCaracService().loadByNombre(1, 10,
					this.estConceptoCaracSearch);
			this.estConceptoCaracListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.estConceptoCaracListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.estConceptoCaracListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.EST_CONCEPTO_CARAC_SEARCH_VIEW;
	}	

  /**
	 * Metodo que permite espeficicar la funcionalidad para la gestion [Crear / Actualizar] de un elmento tipo:
	 * EstConceptoCarac.
	 *     
	 * @author programador.java@consult-soft.com  05-06-2012
	 * @return String Vista a la cual debe retornar
	 * 	 
	 */
	public String manage() {
		String returnView = AppConstants.EST_CONCEPTO_CARAC_SEARCH_VIEW;
		estConceptoCarac.setIdVarSocioeconomica(new Long(estConceptoCarac.getIdVarSocioeconomicaStr()));
		estConceptoCarac.setOrden(new Long(estConceptoCarac.getOrdenStr()));
		estConceptoCarac.setPeso(new Long(estConceptoCarac.getPesoStr()));		
		if(this.estConceptoCarac.getId() == null) {
       returnView = create();
		} else {
       returnView = update();
		}
		return returnView;
	}

	/**
	 * 
	 * @return
	 */
	public String create() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.estConceptoCarac.getId() == null) {
				getEstConceptoCaracService().insert(this.estConceptoCarac);
				msg = AppConstants.SUCCESS_CREATE;
			}
		} catch (EstConceptoCaracException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (EstConceptoCaracRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean(AppConstants.MESSAGE_CONTROLLER_BEAN))
				.setSuccessMessage("- Estudio Socio Economico caracteristicas del concepto " + msg, null, null);
		return AppConstants.EST_CONCEPTO_CARAC_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String update() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.estConceptoCarac.getId() != null) {
				getEstConceptoCaracService().update(this.estConceptoCarac);
				msg = AppConstants.SUCCESS_UPDATE;
			}
		} catch (EstConceptoCaracException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (EstConceptoCaracRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean(AppConstants.MESSAGE_CONTROLLER_BEAN))
				.setSuccessMessage("- Estudio Socio Economico caracteristicas del concepto " + msg, null, null);
		return AppConstants.EST_CONCEPTO_CARAC_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender acciones end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public String createEstConceptoCarac() {
		estConceptoCarac = new EstConceptoCarac();
		estConceptoCarac.setId(null);
		estConceptoCarac.setUsuarioCrea(getLoggedUserID());
		estConceptoCarac.setUsuarioModifica(getLoggedUserID());
		//estConceptoCarac.setEstado("ACTIVO");
		return AppConstants.EST_CONCEPTO_CARAC_CREATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String updateEstConceptoCarac() {
		Map params = getExternalContext().getRequestParameterMap();
		String estConceptoCaracDataIndex = (String) params
				.get("estConceptoCaracDataIndex");
		this.estConceptoCarac = (EstConceptoCarac) ((List) this.estConceptoCaracListModel
				.getWrappedData())
				.get(Integer.parseInt(estConceptoCaracDataIndex));
		return AppConstants.EST_CONCEPTO_CARAC_UPDATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String cancelEstConceptoCarac() {
		return AppConstants.EST_CONCEPTO_CARAC_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- getters and setters init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public EstConceptoCarac getEstConceptoCaracSearch() {
		return estConceptoCaracSearch;
	}

	/**
	 * 
	 * @param estConceptoCaracSearch
	 */
	public void setestConceptoCaracSearch(EstConceptoCarac estConceptoCaracSearch) {
		this.estConceptoCaracSearch = estConceptoCaracSearch;
	}

	/**
	 * 
	 * @return
	 */
	public DataModel getestConceptoCaracListModel() {
		return estConceptoCaracListModel;
	}

	/**
	 * 
	 * @param estConceptoCaracListModel
	 */
	public void setestConceptoCaracListModel(DataModel estConceptoCaracListModel) {
		this.estConceptoCaracListModel = estConceptoCaracListModel;
	}

	/**
	 * 
	 * @return
	 */
	public EstConceptoCarac getEstConceptoCarac() {
		return estConceptoCarac;
	}

	/**
	 * 
	 * @param estConceptoCarac
	 */
	public void setestConceptoCarac(EstConceptoCarac estConceptoCarac) {
		this.estConceptoCarac = estConceptoCarac;
	}

	/**
	 * obtencion del servicio estConceptoCaracService
	 * 
	 * @return
	 */
	public IEstConceptoCaracService getEstConceptoCaracService() {
		return estConceptoCaracService;
	}

	/**
	 * establecimiento del servicio estConceptoCaracService, la inyeccion de la
	 * dependencia la cubre SpringFramework
	 * 
	 * @param estConceptoCaracService
	 */
	public void setEstConceptoCaracService(
			IEstConceptoCaracService estConceptoCaracService) {
		this.estConceptoCaracService = estConceptoCaracService;
	}

	// --------------------------------------------------------------------
	// --- getters and setters end()
	// --------------------------------------------------------------------

}
